package tools;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.awt.image.BufferedImage;
import java.awt.*;

import model.Braille;

public class DrawBraille {

	BufferedImage image;

	public static void main(String[] args) {
		DrawBraille drawBraille = new DrawBraille();
		List<Braille> list =  ChineseToBraille.toBraille("汉语盲字例");
		for(Braille b : list){
			drawBraille.drawOnePic(b,"/Users/ym-hz/Desktop/pic/");
		}
	}
	
	public void drawOnePic(Braille b,String outpath) {
		int imageWidth = 60;//图片的宽度
		int imageHeight = 75;//图片的高度
		image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
		Graphics graphics = image.getGraphics();
		graphics.setColor(Color.WHITE);
		graphics.fillRect(0, 0, imageWidth, imageHeight);
		graphics.setColor(Color.BLACK);
		if(b.getONE())	
			graphics.fillOval(10, 10, 10, 10);
		else
			graphics.drawOval(10, 10, 9, 9);
		if(b.getTWO())
			graphics.fillOval(10, 25, 10, 10);
		else
			graphics.drawOval(10, 25, 9, 9);
		if(b.getTHREE())
			graphics.fillOval(10, 40, 10, 10);
		else
			graphics.drawOval(10, 40, 9, 9);
		if(b.getFOUR())
			graphics.fillOval(10, 55, 10, 10);
		else
			graphics.drawOval(10, 55, 9, 9);
		if(b.getFIVE())
			graphics.fillOval(25, 55, 10, 10);
		else
			graphics.drawOval(25, 55, 9, 9);
		if(b.getSIX())
			graphics.fillOval(25, 40, 10, 10);
		else
			graphics.drawOval(25, 40, 9, 9);
		if(b.getSEVEN())
			graphics.fillOval(25, 25, 10, 10);
		else
			graphics.drawOval(25, 25, 9, 9);
		if(b.getEIGHT())
			graphics.fillOval(25, 10, 10, 10);
		else
			graphics.drawOval(25, 10, 9, 9);
		if(b.getNINE())
			graphics.fillOval(40, 10, 10, 10);
		else
			graphics.drawOval(40, 10, 9, 9);
		if(b.getZERO())
			graphics.fillOval(40, 25, 10, 10);
		else
			graphics.drawOval(40, 25, 9, 9);
		if(b.getA())
			graphics.fillOval(40, 40, 10, 10);
		else
			graphics.drawOval(40, 40, 9, 9);
		if(b.getB())
			graphics.fillOval(40, 55, 10, 10);
		else
			graphics.drawOval(40, 55, 9, 9);
		graphics.dispose();
		System.out.println("输出到:"+outpath+b.getSrc()+".jpg");
		createImage(outpath+b.getSrc()+".jpg");
	}

	public void createImage(String fileLocation) {
		try {
			FileOutputStream fos = new FileOutputStream(fileLocation);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
			encoder.encode(image);
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}